﻿using System;
using Besture.Bll;
using Besture.Utils;

namespace Besture.UI.Admin
{
    public partial class Login : System.Web.UI.Page
    {
        private string returnUrl = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            returnUrl = Fun.Query("returnUrl");
            if (string.IsNullOrEmpty(returnUrl))
            {
                returnUrl = AdminService.ADMIN_INDEX;
            }
        }

        protected void btnLogin_Click(object sender, EventArgs eventArgs)
        {
            //提示
            lblTips.Text = "提示：正在加载中...";
            //错误提示
            const string nullTips = "提示：请输入用户名或密码！";
            const string tips = "提示：用户名或密码输入有误！";
            string loginName = txtLoginName.Text;
            string password = Fun.GetMD5(txtPassword.Text);
            if (string.IsNullOrEmpty(loginName) || string.IsNullOrEmpty(password))
            {
                lblTips.Text = nullTips;
                return;
            }
            AdminUserDac adminUserDac = new AdminUserDac();
            //用户登录
            bool loginFlag = adminUserDac.Login(loginName, password);
            if (loginFlag)
            {
                lblTips.Text = "提示：登录成功，正在跳转，请稍等...";
                //添加登录日志
                string logContent = string.Format(@"管理用户{0}登录成功", loginName);
                AdminService.AddLog(logContent, LogModules.USER);
                Response.Redirect(returnUrl);
            }
            else
            {
                lblTips.Text = tips;
            }
        }
    }
}